(旧Siv3D) 押されたキーの名前を表示
https://gyazo.com/0b6fd6b16e4fd6fd14869246611f51c4
code:keyname.cpp
String join(const Array<String>& arr, const String& sep)
{
String joined = L"";
for (const auto& str : arr)
{
if (joined.length)
{
joined += sep;
}
joined += str;
}
return joined;
}
template <class Container, class T>
bool exists(const Container& c, const T& val)
{
return std::find(c.begin(), c.end(), val) != c.end();
}
// 仮想キーコードに対応するキーの名前を取得する
String getKeyName(const uint8 vkcode)
{
const Array<uint8> vkext = {
VK_INSERT, VK_DELETE, VK_HOME, VK_END, VK_PRIOR, VK_NEXT, VK_UP, VK_DOWN, VK_LEFT, VK_RIGHT,
};
const LONG scancode = ::MapVirtualKeyW(vkcode, MAPVK_VK_TO_VSC) | (exists(vkext, vkcode) ? 0x100 : 0);
if (::GetKeyNameTextW((scancode << 16) | (1 << 25), name, sizeof(name)) == 0)
{
return L"";
}
return name;
}
void Main()
{
const Font font(48, Typeface::Bold);
// 押されたキー一覧
Array<String> keynames;
while (System::Update())
{
if (Input::AnyKeyClicked())
{
keynames.clear();
for (const auto& key : Input::GetActiveKeys())
{
const String name = getKeyName(key.code);
if (!exists(keynames, name)) //"Ctrl"などの重複の回避
{
keynames.push_back(name);
}
}
}
font(join(keynames, L", ")).drawCenter(Window::Center());
}
}